/*
 * ESUndefined.java - ScriptME
 * 
 * Copyright (c) 2009 Cesar Henriques <cesar at alttab.com.ar>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Based on FESI Project
 * 
 * Contributors:
 * 	Jean-Marc Lugrin - initial API and implementation
 * 	Cesar Henriques <cesar at alttab.com.ar> - J2ME Porting and Extensions
 */
package org.scriptme.ecmascript.data;

import org.scriptme.ecmascript.exceptions.EcmaScriptException;

/**
 * Implements the Undefined primitive value.
 * <P>
 * There is a single Undefined value reached by ESUndefined.theUndefined
 * <P>
 * The primitive Undefined, null and the java null are not equivallent and must
 * be used in the appropriate context
 */
public final class ESUndefined extends ESPrimitive {

	/** the READ-ONLY undefined value. */
	public static ESUndefined theUndefined = new ESUndefined();

	/**
	 * Instantiates a new eS undefined.
	 */
	private ESUndefined() {
	}

	/**
	 * Implements a specific error message if an undfined value is called.
	 * 
	 * @param thisObject
	 *            The object on which the call is made
	 * @param arguments
	 *            The arguments of the function
	 * 
	 * @return never
	 * 
	 * @throws EcmaScriptException
	 *             the ecma script exception
	 * 
	 * @exception EcmaScriptException
	 *                Thrown to indicate call on undefined value
	 */
	public ESValue callFunction(ESObject thisObject, ESValue[] arguments)
			throws EcmaScriptException {
		throw new EcmaScriptException(
				"Function called on undefined value or property");
	}

	/**
	 * Implements a specific error message if an undfined value is called via
	 * new.
	 * 
	 * @param thisObject
	 *            The object on which the call is made
	 * @param arguments
	 *            The arguments of the function
	 * 
	 * @return never
	 * 
	 * @throws EcmaScriptException
	 *             the ecma script exception
	 * 
	 * @exception EcmaScriptException
	 *                Thrown to indicate call on undefined value
	 */
	public ESObject doConstruct(ESObject thisObject, ESValue[] arguments)
			throws EcmaScriptException {
		throw new EcmaScriptException(
				"'new' called on undefined value or property");
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESValue#toDetailString()
	 */
	public String toDetailString() {
		return "ES:<undefined>";
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESValue#getTypeOf()
	 */
	public int getTypeOf() {
		return EStypeUndefined;
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESValue#getTypeofString()
	 */
	public String getTypeofString() {
		return "undefined";
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return "undefined";
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESValue#doubleValue()
	 */
	public double doubleValue() {
		return Double.NaN;
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESValue#booleanValue()
	 */
	public boolean booleanValue() {
		return false;
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESValue#toJavaObject()
	 */
	public Object toJavaObject() {
		return null; // should throw an error
	}
}
